package android.magical;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.magical.MainActivity.MyAdapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ViewEventFragment extends Fragment {
	private final String TAG = "ViewEventFragment";
	private TextView mDate, mTime, mCreator, mMessage;
	private Button mDelete;
	static String date, time, owner, message;
	private MainActivity mActivity;
	int id;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  Log.v(TAG, "Created.");
	  
	}
	
	public static String getMessage(){
		return date+"&"+time+"&"+owner+"&"+message;
	}
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
	// Inflate the layout for this fragment
	View v = inflater.inflate(R.layout.view_event, container, false);
	mDate = (TextView)v.findViewById(R.id.date);
	mTime = (TextView)v.findViewById(R.id.time);
	mCreator = (TextView)v.findViewById(R.id.owner);
	mMessage = (TextView)v.findViewById(R.id.message);
	mDelete = (Button)v.findViewById(R.id.delete);
	
	
	GregorianCalendar calendar = ((MainActivity)getActivity()).getChosenDate();
	date = calendar.get(Calendar.DAY_OF_MONTH) + "/" + 
					(calendar.get(Calendar.MONTH)+1) + "/" + 
					calendar.get(Calendar.YEAR);
	time = getArguments().getString("time");
	owner = getArguments().getString("owner");
	message = getArguments().getString("message");
	id = getArguments().getInt("id");
	
	mActivity = (MainActivity)getActivity();
	
	
	mDelete.setOnClickListener(new OnClickListener(){
		public void onClick(View v){
			deleteEvent();
		}
	});
	
	mDate.setText(Html.fromHtml("<font color=\"#0099CC\">Date: </font>" + date));
	mTime.setText(Html.fromHtml("<font color=\"#0099CC\">Time: </font>" + time));
	mCreator.setText(Html.fromHtml("<font color=\"#0099CC\">Creator: </font>" + owner));
	mMessage.setText(message);
	return v;
	}
    
    private void deleteEvent(){
		mActivity.getDatabase().deleteItem(id);
		Toast.makeText(mActivity, "Deleted an event", Toast.LENGTH_SHORT).show();
		FragmentManager fm = mActivity.getSupportFragmentManager();
		fm.popBackStack();
		FragmentTransaction tr = fm.beginTransaction();
		EventsFragment events = new EventsFragment();
		tr.replace(R.id.events_fragment, events, "EventsFragment");
		//tr.addToBackStack("EventsFragment");			
		mActivity.restoreMainView();
		ViewPager pager = mActivity.getPager();
		MyAdapter adapter = ((MyAdapter)pager.getAdapter());
		adapter.notifyDataSetChanged();
    }
}
